#include "MgDesktop.h"
#include <fstream>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TextOutputter.h>
#include <cppunit/XmlOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main(int argc, char** argv)
{
    ACE_DEBUG((LM_INFO, ACE_TEXT("Initialize Platform.ini\n")));
    //Benchmark this
#ifdef WIN32
    long lStart = GetTickCount();
#endif
    MgPlatform::Initialize(L"Platform.ini");
#ifdef WIN32
    ACE_DEBUG((LM_INFO, ACE_TEXT("Platform Initialization time in %dms\n"), (GetTickCount()-lStart)));
#endif

    CppUnit::TextUi::TestRunner runner;

    // Add all of the tests
#ifdef _DEBUG
    ACE_DEBUG((LM_INFO, ACE_TEXT(">>>>> Running all unit tests - Excluding Performance and CoordinateSystem. <<<<<\n\n")));
#else
    ACE_DEBUG((LM_INFO, ACE_TEXT(">>>>> Running all unit tests - Excluding Performance. <<<<<\n\n")));
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestCoordinateSystem").makeTest());
#endif
    runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestResourceService").makeTest());
    runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestFeatureService").makeTest());
    
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestGeometry").makeTest());
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestLogManager").makeTest());
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestMdfModel").makeTest());
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestKmlService").makeTest());
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestMappingService").makeTest());
    
    runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestRenderingService").makeTest());
    //This is currently failing because runtime map (de)serialization for cloning is broken
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestTileService").makeTest());
    
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestServerAdminService").makeTest());
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestServerManager").makeTest());
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestServiceManager").makeTest());
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestSiteService").makeTest());
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestSiteManager").makeTest());
    //runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("TestTransformMesh").makeTest());

    STRING fileName = L"UnitTestResults.xml";
    if (fileName.size() > 0)
    {
        ofstream outfile(MG_WCHAR_TO_CHAR(fileName.c_str()));

        if (outfile.is_open())
        {
            runner.setOutputter(new CppUnit::XmlOutputter(&runner.result(), outfile, "ISO-8859-1"));
            runner.run();
            outfile.close();
        }
    }
    else
    {
        runner.setOutputter(new CppUnit::TextOutputter(&runner.result(), std::cout));
        runner.run();
    }

    int nResult = runner.result().testFailuresTotal();

    return nResult;
}